﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Experimentor
{
    public partial class form_LoadSlides : Form
    {
        private string basefile;
        private DataGridViewColumnCollection columns;

        public form_LoadSlides(string basefile, DataGridViewColumnCollection columns)
            : base()
        {
            this.basefile = basefile;
            this.columns = columns;
            InitializeComponent();
            DataTable dt_columns = new DataTable("columns");
            dt_columns.Columns.Add("Keys", typeof(int));
            dt_columns.Columns.Add("Values");
            for (int i = 0; i < this.columns.Count; i++) if (this.columns[i].HeaderText != "Order") dt_columns.Rows.Add(i, columns[i].HeaderText);
            this.comboBox_Slide.DataSource = dt_columns;
            this.comboBox_Slide.DisplayMember = "Values";
            this.comboBox_Slide.ValueMember = "Keys";
        }

        private void button_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (this.basefile != null) ofd.InitialDirectory = Path.GetDirectoryName(this.basefile);
            ofd.Multiselect = true;
            DialogResult dr = ofd.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                Uri _mainpath = new Uri(this.basefile, UriKind.Absolute);
                foreach (string filename in ofd.FileNames)
                {
                    this.listBox_Files.Items.Add(Uri.UnescapeDataString(_mainpath.MakeRelativeUri(new Uri(filename, UriKind.Absolute)).ToString()));
                }
            }
        }

        private void textBox_ToSet_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse((sender as TextBox).Text, out num)) this.checkBox_Repeat.Enabled = true;
            else this.checkBox_Repeat.Enabled = false;
        }

        private void button_Remove_Click(object sender, EventArgs e)
        {
            for (int i = this.listBox_Files.SelectedIndices.Count - 1; i >= 0; i--) this.listBox_Files.Items.RemoveAt(this.listBox_Files.SelectedIndices[i]);
        }

        private void listBox_Files_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete) this.button_Remove_Click(sender, new EventArgs());
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }


    }
}
